<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <!-- 
        生成器函数是 ES6 提供的一种异步变成解决方案,语法行为与传统函数完全不同

     -->
     <!-- https://blog.csdn.net/weixin_46246000/article/details/121095396 -->
     <!-- https://baijiahao.baidu.com/s?id=1672510039262856665&wfr=spider&for=pc -->
    <script>

        //模拟获取 用户数据 订单数据 商品数据
        function getUsers () {
            setTimeout(()=>{
                let data = '用户数据'
                //调用next方法,并且将数据传入
                iterator.next(data)
            }, 1000)
        }


        function getOrders () {
            setTimeout(()=>{
                let data = '订单数据'
                iterator.next(data)
            }, 1000)
        }

        function getGoods () {
            setTimeout(()=>{
                let data = '商品数据'
                iterator.next(data)
            }, 1000)
        }

        function * gen() {

            let users = yield getUsers()
            console.log(users)

            let orders = yield getOrders()
            console.log(orders)

            let goods = yield getGoods()
            console.log(goods)
        }

        let iterator = gen()
        iterator.next()


    </script>
</body>
</html>